<?php
namespace plugins\site_config\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\site_config\model\SiteInfoModel;
use plugins\site_config\lib\Common as LCOM;

//站点配置首页
class AdminIndexController extends PluginAdminBaseController
{
    //使用教程
    public function useCourse()
    {
        $text = getModuleConfig('site_config','config','member_use_rule.config');
        $text = htmlspecialchars_decode($text);
        if($this->request->isPost()){
            $post = $this->request->post();

            saveModuleConfigData('site_config','config','member_use_rule.config',$post['data']['edit']);

            return api_json_echo(true,'修改完成');
        }

        $this->assign('text',$text);

        return $this->fetch();
    }
    //站点信息
    public function index()
    {
        $siteInfo = LCOM::getSiteInfo();

        $this->assign( 'site' , $siteInfo );
        return $this->fetch();
    }

    //post save
    public function saveSiteInfo()
    {
        $data = $this->request->param();
        
        if( !LCOM::setSiteInfo( $data [ 'data' ] ) ){
            return api_json_echo( false , '保存失败,请稍后再试' );
        }
        return api_json_echo( true , '保存成功' );
    }
    
    //上传支付方式的图标
    public function logoIcon()
    {
        $data = $this->request->param();
        if( empty( $_FILES ) ){
            return api_json_echo( false , '未获取到数据' );
        }
        $path = '/plugins/site_config/config/logo';
        $rule = [ 
            'size' => 1024*1024,    //1024kb
            'ext' =>['jpeg' , 'png' , 'jpg' ],
            'type' => [ 'image/jpeg' , 'image/png' , 'image/jpg' ]
        ];
        
        $res = uploadImg( $_FILES [ 'file' ] , $path , $rule );
        
        if( !$res[ 'status' ] ){
            return api_json_echo( false , '上传失败' , $res [ 'message' ] , $res [ 'code' ] );
        }
        return api_json_echo( true , '上传成功' ,  $res [ 'data' ] );
    }
    
}